home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xfig.idb / usr / freeware / src / xfig / transfig.3.1.2 / fig2dev / getopt.c.z / getopt.c
Encoding:
C/C++ Source or Header  |  1997-09-09  |  3.3 KB  |  108 lines

  1. /*
  2.  * TransFig: Facility for Translating Fig code
  3.  * Copyright (c) 1985 Supoj Sutantavibul
  4.  * Copyright (c) 1991 Micah Beck
  5.  *
  6.  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  7.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  8.  * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  9.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  10.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  11.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  12.  * PERFORMANCE OF THIS SOFTWARE.
  13.  *
  14.  * The X Consortium, and any party obtaining a copy of these files from
  15.  * the X Consortium, directly or indirectly, is granted, free of charge, a
  16.  * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
  17.  * nonexclusive right and license to deal in this software and
  18.  * documentation files (the "Software"), including without limitation the
  19.  * rights to use, copy, modify, merge, publish, distribute, sublicense,
  20.  * and/or sell copies of the Software, and to permit persons who receive
  21.  * copies from any such party to do so, with the only requirement being
  22.  * that this copyright notice remain intact.  This license includes without
  23.  * limitation a license to do the foregoing actions under any patents of
  24.  * the party supplying this software to the X Consortium.
  25.  */
  26.  
  27. /*
  28.     I got this off net.sources from Henry Spencer.
  29.     It is a public domain getopt(3) like in System V.
  30.     I have made the following modifications:
  31.  
  32.     index(s,c) was added because too many people could
  33.     not compile getopt without it.
  34.  
  35. */
  36. #include <stdio.h>
  37.  
  38. #ifndef lint
  39. static    char    sccsfid[] = "@(#) getopt.c 5.0 (UTZoo) 1985";
  40. #endif
  41.  
  42. #define    ARGCH    (int)':'
  43. #define BADCH     (int)'?'
  44. #define EMSG     ""
  45. #define    ENDARGS  "--"
  46.  
  47. /* this is included because index is not on some UNIX systems */
  48. static
  49. char *
  50. index (s, c)
  51. register    char    *s;
  52. register    int     c;
  53.     {
  54.     while (*s)
  55.         if (c == *s) return (s);
  56.         else s++;
  57.     return (NULL);
  58.     }
  59.  
  60. /*
  61.  * get option letter from argument vector
  62.  */
  63. int    opterr = 1,        /* useless, never set or used */
  64.     optind = 1,        /* index into parent argv vector */
  65.     optopt;            /* character checked for validity */
  66. char    *optarg;        /* argument associated with option */
  67.  
  68. #define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  69.         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  70.  
  71. fig_getopt(nargc,nargv,ostr)
  72. int    nargc;
  73. char    **nargv,
  74.     *ostr;
  75. {
  76.     static char    *place = EMSG;    /* option letter processing */
  77.     register char    *oli;        /* option letter list index */
  78.     char    *index();
  79.  
  80.     if(!*place) {            /* update scanning pointer */
  81.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  82.         if (*place == '-') {    /* found "--" */
  83.             ++optind;
  84.             return(EOF);
  85.         }
  86.     }                /* option letter okay? */
  87.     if ((optopt = (int)*place++) == ARGCH || !(oli = index(ostr,optopt))) {
  88.         if(!*place) ++optind;
  89.         tell(": illegal option -- ");
  90.     }
  91.     if (*++oli != ARGCH) {        /* don't need argument */
  92.         optarg = NULL;
  93.         if (!*place) ++optind;
  94.     }
  95.     else {                /* need an argument */
  96.         if (*place) optarg = place;    /* no white space */
  97.         else if (nargc <= ++optind) {    /* no arg */
  98.             place = EMSG;
  99.             tell(": option requires an argument -- ");
  100.         }
  101.          else optarg = nargv[optind];    /* white space */
  102.         place = EMSG;
  103.         ++optind;
  104.     }
  105.     return(optopt);            /* dump back option letter */
  106. }
  107.  
  108.